<html>
<head><meta charset="utf-8"><title>aggregate arbitrary constants at compile-time into a sing... · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html">aggregate arbitrary constants at compile-time into a sing...</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228918016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228918016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228918016">(Mar 05 2021 at 08:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/Filter.20out.20query.20machinery.20from.20compiler.20.E2.80.A6.20compiler-team.23410/near/228753293">said</a>:</p>
<blockquote>
<p>it's pretty much exactly that, except imo it's less hacky than the <code>linkme</code> crate, as it does not rely on linker functionality. It's the MVP needed for <a href="https://github.com/rust-lang/const-eval/issues/25#issuecomment-533840428">https://github.com/rust-lang/const-eval/issues/25#issuecomment-533840428</a> (which itself is desirable for custom test harnesses, tracing metadata and custom const-evaluated artifacts).</p>
</blockquote>
<p>I'm just saying that feels like a major language (CTFE) feature on its own right that requires an RFC. There's a large design space here. I was very surprised to see this "en passant" in what is superficially an unrelated proposal.^^</p>



<a name="228921712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228921712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228921712">(Mar 05 2021 at 09:13)</a>:</h4>
<p>well... it's going to be a really unstable rustc attribute with no intention of getting stabilized. I thought this a situation like <code>rustc_scalar_range_start</code> which is the attribute that gives us the <code>NonZero</code> types</p>



<a name="228922220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228922220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228922220">(Mar 05 2021 at 09:18)</a>:</h4>
<p>that attribute is a very thin layer on top of an already existing rustc feature</p>



<a name="228922230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228922230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228922230">(Mar 05 2021 at 09:18)</a>:</h4>
<p>this here, OTOH, will require some major infrastructure</p>



<a name="228923032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228923032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228923032">(Mar 05 2021 at 09:25)</a>:</h4>
<p>the "only" new thing is the generation of the constant in the end. We already have such "attribute collection queries" (<code>rustc_diagnostic_item</code> works pretty much the same way). I agree that the nice way would be to first RFC a proper feature here, but it also feels like a feature I don't really want to RFC in the next year or so as wg-const-eval has enough unfinished features on its plate</p>



<a name="228928834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228928834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228928834">(Mar 05 2021 at 10:11)</a>:</h4>
<p>I'm not sure if this is related, but I really would love to see "a built-in version of <code>linkme</code>": <a href="https://github.com/rust-lang/rust/issues/82371#issuecomment-784421726">https://github.com/rust-lang/rust/issues/82371#issuecomment-784421726</a></p>



<a name="228931856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228931856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228931856">(Mar 05 2021 at 10:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/Filter.20out.20query.20machinery.20from.20compiler.20.E2.80.A6.20compiler-team.23410/near/228922220">said</a>:</p>
<blockquote>
<p>that attribute is a very thin layer on top of an already existing rustc feature</p>
</blockquote>
<p>also remember that this feature was unsound for quite some time until we fixed the unsafety checker. So it's not a great example for "ad-hoc language extension gone well".^^</p>



<a name="228932084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228932084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228932084">(Mar 05 2021 at 10:39)</a>:</h4>
<blockquote>
<p>the "only" new thing is the generation of the constant in the end.</p>
</blockquote>
<p>We need at least two mechanisms that work together: to "register" an element to be part of this slice (const-eval with side-effects, that sounds tricky), and then at link time something to collect all pieces of the slice and put them into the original slice variable. We need to ensure that CTFE doesn't read from these slices as that would create funny cycles. I think there are quite a few subtleties here.</p>



<a name="228935069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935069">(Mar 05 2021 at 11:06)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="233931" href="/#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/Filter.20out.20query.20machinery.20from.20compiler.20.E2.80.A6.20compiler-team.23410">#t-compiler/major changes &gt; Filter out query machinery from compiler … compiler-team#410</a> by <span class="user-mention silent" data-user-id="124288">oli</span></p>



<a name="228935363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935363">(Mar 05 2021 at 11:09)</a>:</h4>
<p>Well... for this initial impl I wasn't actually going to have constants being registered, but instead collected the functions' <code>ty::Instance</code> directly (at monomorphization time). The only interaction with CTFE would have been a forever unstable intrinsic that returns a slice of <code>*const ()</code>. In <code>rustc_driver</code> I would have then done something like <code>static FOO: &amp;[*const ()] = that_intrinsic();</code></p>



<a name="228935423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935423">(Mar 05 2021 at 11:09)</a>:</h4>
<p>so there's no way to create a cycle, as you only get that one static in the end, and it doesn't actually refer to other statics</p>



<a name="228935491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935491">(Mar 05 2021 at 11:10)</a>:</h4>
<p>you can't mark that static in a way that would get it collected for itself</p>



<a name="228935652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935652">(Mar 05 2021 at 11:10)</a>:</h4>
<p>of course the real feature we want here is to have an attribute that can be applied to constants, and that collects the result of those constants and allows another constant to be created from those collected results</p>



<a name="228935718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228935718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228935718">(Mar 05 2021 at 11:11)</a>:</h4>
<p>but as you noted, that has lots of pitfalls that need to be considered</p>



<a name="228936587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228936587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228936587">(Mar 05 2021 at 11:18)</a>:</h4>
<blockquote>
<p>also remember that this feature was unsound for quite some time until we fixed the unsafety checker. So it's not a great example for "ad-hoc language extension gone well".^^</p>
</blockquote>
<p>well... in this case the worst case outcome is badly printed backtraces when using <code>RUST_BACKTRACE=1</code> instead of <code>RUST_BACKTRACE=full</code>... and only the backtraces of compiler ICEs are changed at all</p>



<a name="228994759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228994759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228994759">(Mar 05 2021 at 17:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E/near/228935363">said</a>:</p>
<blockquote>
<p>Well... for this initial impl I wasn't actually going to have constants being registered, but instead collected the functions' <code>ty::Instance</code> directly (at monomorphization time). The only interaction with CTFE would have been a forever unstable intrinsic that returns a slice of <code>*const ()</code>. In <code>rustc_driver</code> I would have then done something like <code>static FOO: &amp;[*const ()] = that_intrinsic();</code></p>
</blockquote>
<p>I see, and the intrinsic would walk the crate tree and gather that list of functions?</p>



<a name="228994831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228994831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228994831">(Mar 05 2021 at 17:47)</a>:</h4>
<p>oh no... "at monomorphization time", how is that supposed to work? can CTFE be delayed until all monomorphization is done?</p>



<a name="228995438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228995438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228995438">(Mar 05 2021 at 17:51)</a>:</h4>
<p>well... yea, that is a problem. It won't work for the current crate ^^ you can only collect things from other crates</p>



<a name="228995476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/228995476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#228995476">(Mar 05 2021 at 17:51)</a>:</h4>
<p>but, that is fine for the rustc_driver case</p>



<a name="229002655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/229002655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#229002655">(Mar 05 2021 at 18:36)</a>:</h4>
<p>I dont see how "its in other crates" helps; rustc_driver might well be monomorphizing rustc_mir things</p>



<a name="229004607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/229004607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#229004607">(Mar 05 2021 at 18:49)</a>:</h4>
<p>hmm that is true</p>



<a name="229004742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/229004742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#229004742">(Mar 05 2021 at 18:50)</a>:</h4>
<p>I guess for the local crate we'd have to do something hacky like create an <code>AllocId</code> but not actually back it with data and then fill it in after collection</p>



<a name="230130025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/aggregate%20arbitrary%20constants%20at%20compile-time%20into%20a%20sing.../near/230130025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/aggregate.20arbitrary.20constants.20at.20compile-time.20into.20a.20sing.2E.2E.2E.html#230130025">(Mar 13 2021 at 03:03)</a>:</h4>
<p>Just pass by to say i'm a fan of this feature. Currently i'm using <code>linkme</code> in my own crates, which triggers some seemingly hard-to-fix bugs of the LLVM interaction, and i have to use workarounds. I really wish this is part of the language.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>